﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PTTK19.DB;

namespace PTTK19.GUI
{
    public partial class ChiTietMonAnFrm : Form
    {
        private int ID;
        private int _index;
        public ChiTietMonAnFrm(int id)
        {
            ID = id;
            InitializeComponent();
        }

        private void ChiTietMonAnFrm_Load(object sender, EventArgs e)
        {
            txtMaThucDon.Text = ID.ToString();
            LoadDgvMonAn();

            btnXoaList.Enabled = false;
            btnThem.Enabled = true;
        }

        private void LoadDgvMonAn()
        {
            DataTable dt = DBMonAn.GetList();
            dgvAll.DataSource = dt;
            DataTable dt2 = DBMonAn.ChiTietMonAn(ID);
            dgvAdd.DataSource = dt2;
        }

        private void dgvAll_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                string command = dgvAll.Columns[e.ColumnIndex].Name;
                if (command == "ChiTiet") // colbtn là tên cột chứa button
                {
                    MonAnFrm ctFrm = new MonAnFrm();
                    //ctFrm.MaNguyenLieu = _MaNguyenLieu;
                    ctFrm._MaMonAn = (int)dgvAll.Rows[e.RowIndex].Cells[1].Value;
                    ctFrm.ShowDialog();
                }
            }
        }

        private void dgvAll_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int index = dgvAll.CurrentRow.Index;
                if (dgvAll.CurrentRow != null)
                {
                    //lay ma mon an
                    _index = index;
                    txtMaMonAn.Text = ((int)dgvAll.Rows[index].Cells[1].Value).ToString();
                    txtTenMonAn.Text = (string)dgvAll.Rows[index].Cells[2].Value;
                }
            }
            catch (Exception ex)
            { }

            btnXoaList.Enabled = false;
            btnThem.Enabled = true;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            int _MaMonAn = 0;
            int _MaThucDon = ID;
            int index = dgvAll.CurrentRow.Index;
            if (dgvAll.CurrentRow != null)
            {
                //lay thong tin chi tiet nguyen lieu
                _MaMonAn = (int)dgvAll.Rows[index].Cells[1].Value;
            }
            DBChiTietThucDonMonAn.ThemMonAn(_MaThucDon, _MaMonAn);
            LoadDgvMonAn();
        }

        private void btnXoaList_Click(object sender, EventArgs e)
        {
            int _MaMonAn= 0;
            int _MaThucDon = ID;
            int index = dgvAdd.CurrentRow.Index;
            if (dgvAdd.CurrentRow != null)
            {
                //lay thong tin chi tiet nguyen lieu
                _MaMonAn = (int)dgvAdd.Rows[index].Cells[0].Value;
            }
            DBChiTietThucDonMonAn.BotMonAn(_MaThucDon, _MaMonAn);
            LoadDgvMonAn();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            dgvAdd.Rows.Clear();
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Cập nhật thành công");
        }

        private void dgvAdd_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int index = dgvAdd.CurrentRow.Index;
                if (dgvAdd.CurrentRow != null)
                {
                    //lay ma mon an
                    _index = index;
                    txtMaMonAn.Text = ((int)dgvAdd.Rows[index].Cells[1].Value).ToString();
                    txtTenMonAn.Text = (string)dgvAdd.Rows[index].Cells[2].Value;
                }
            }
            catch (Exception ex)
            { }
            btnXoaList.Enabled = true;
            btnThem.Enabled = false;
        }
    }
}
